<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:p="http://primefaces.org/ui"
                xmlns:util="http://java.sun.com/jsf/composite/components"
                template="../../template/tagedu.xhtml">

    <ui:define name="title">
        <title><h:outputText value="TAG - Cadastro de Ator"/></title>
    </ui:define>

    <ui:define name="content">
        <h:outputText styleClass="title" value="Cadastro de #{newActorMB.personage.name}"/>
        
        <h:panelGroup layout="block">
            
            <p:selectOneMenu />
            
            <p:fieldset legend="Novo" toggleable="true" collapsed="true" >
                <h:form id="newHumanF">
                    <p:messages id="newMsg"/>
                    <util:humanRegister id="newHuman" human="#{newActorMB.human}" button="#{newActorMB.save}"/>
                    <p:commandButton value="Cadastrar" process="@form" update="@form"
                                     action="#{newActorMB.save}" ajax="true"/>
                </h:form>
            </p:fieldset>
            
            <p:fieldset legend="Pesquisar" toggleable="true" collapsed="true">
                <h:form id="findHumanF">
                    <p:messages id="findMsg"/>
                    <h:panelGroup id="humanSearchResultGroup">
                        <p:dataTable id="humanTable" emptyMessage="Sem resultados" paginator="true" rows="10"
                                paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                                rowsPerPageTemplate="10,25,50" value="#{newActorMB.humanList}"
                                var="human" rowKey="#{human.id}" selection="#{newActorMB.human}">
                            <p:column selectionMode="single"/>
                            <p:column rendered="false">
                                <h:outputText value="#{human.id}"/>
                            </p:column>
                            <p:column>
                                <p:rowToggler/>
                            </p:column>
                            <p:column filterBy="#{human.name}"
                                    filterMatchMode="contains"
                                    style="width:490px;display:block;"
                                    headerText="Nome">
                                <h:outputText value="#{human.name}"/>
                            </p:column>
                            <p:rowExpansion>
                                <h:panelGrid columns="2" cellpadding="4">
                                    <h:outputLabel value="Nascimento: " for="birthday"/>
                                    <h:outputText value="#{human.birthday.time}">
                                        <f:convertDateTime pattern="dd/MM/yyyy"/>
                                    </h:outputText>
                                    <h:outputLabel value="Endereço: " for="address"/>
                                    <h:outputText value="#{human.address.street.name}, #{human.address.number}.
                                                #{human.address.complement}. #{human.address.zipcode.city.name}/#{human.address.zipcode.city.state.name}"/>
                                    <ui:repeat value="#{human.additionalInfos}" var="addInfo">
                                        <h:outputLabel value="#{addInfo.additionalInfoType.acronym}: "/>
                                        <h:outputText value="#{addInfo.value}"/>
                                    </ui:repeat>
                                </h:panelGrid>
                            </p:rowExpansion>
                        </p:dataTable>
                    </h:panelGroup>
                        
                    <h:panelGroup styleClass="pgroupActor" id="actorDataGroup">
                        <h:panelGroup styleClass="panelGrids">
                            <h:outputLabel for="activated" value="Ativação*:"/>
                            <p:inputMask id="activated" mask="99/99/9999" value="#{newActorMB.actor.activated.time}" 
                                        required="true" requiredMessage="'Ativação' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>

                        </h:panelGroup>    
                        <br/>
                        <h:panelGroup id="deactivatedGroup">
                            <h:outputLabel id="deactivatedLabel" rendered="#{not newActorMB.deactivatedIsNull}" for="deactivated" value="Desativação:"/>
                            <p:inputMask id="deactivated"
                                        mask="99/99/9999" rendered="#{not newActorMB.deactivatedIsNull}" 
                                        value="#{newActorMB.actor.deactivated.time}"
                                        required="#{not newActorMB.deactivatedIsNull}" 
                                        requiredMessage="'Desativação' é obrigatório">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </p:inputMask>                        
                        </h:panelGroup>
                        <br/>
                        <h:outputLabel value="Sem data de desativação:" for="deactivatedIsNull"/>
                        <p:selectBooleanCheckbox  id="deactivatedIsNull" value="#{newActorMB.deactivatedIsNull}">
                            <p:ajax onstart="statusDialog.show();" onsuccess="statusDialog.hide();"
                                    process="deactivatedIsNull" update="deactivatedGroup"/>
                        </p:selectBooleanCheckbox>
                            <br/>
                            <span class="clear"></span>
                            <h:outputLabel for="unity2" value="Local*:"/>
                            <h:selectOneMenu id="unity2" value="#{newActorMB.unity}" converter="unityC"
                                            required="true" requiredMessage="'Local' é obrigatório">
                                <f:selectItems value="#{newActorMB.findUnitys}" var="unity" itemLabel="#{unity.name}"/>
                            </h:selectOneMenu>

                    </h:panelGroup>
                    <br/>
                    <p:commandButton onstart="statusDialog.show();" onsuccess="statusDialog.hide();"
                                    value="Cadastrar" ajax="true" process="@form" update="@form"
                                    action="#{newActorMB.createActor()}"/>
                </h:form>
            </p:fieldset>
        </h:panelGroup>
    </ui:define>

    <ui:define name="modals">
        <p:dialog id="searchHumanDialog" position="top" modal="true" 
                  widgetVar="searchHuman" closable="true" 
                  resizable="false" width="1000" height="500">
            <h:form>
                <p:dataTable value="#{newActorMB.humanList}" var="parent" paginator="true" rows="10"
                             paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                             rowsPerPageTemplate="10,25,50"
                             selection="#{newActorMB.parent}" rowKey="#{parent.id}">
                    <p:column selectionMode="single" />
                    <p:column rendered="false">
                        <h:outputText value="#{parent.id}"/>
                    </p:column>
                    <p:column>
                        <p:rowToggler/>
                    </p:column>

                    <p:column filterMatchMode="contains" filterStyle="width: 200px" 
                              filterBy="#{parent.name}">
                        <f:facet name="header">
                            <h:outputText value="Nome"/>
                        </f:facet>
                        <h:outputText value="#{parent.name}"/>
                    </p:column>
                    <p:rowExpansion>
                        <h:panelGrid columns="2" cellpadding="4">
                            <h:outputLabel value="Nascimento: " for="birthday"/>
                            <h:outputText value="#{parent.birthday.time}" id="birthday">
                                <f:convertDateTime pattern="dd/MM/yyyy"/>
                            </h:outputText>
                            <h:outputLabel value="Endereço: " for="address"/>
                            <h:outputText value="#{parent.address.street.name}, #{parent.address.number}.
                                          #{parent.address.complement}. #{parent.address.zipcode.city.name}/#{parent.address.zipcode.city.state.name}" id="address"/>
                            <ui:repeat value="#{parent.additionalInfos}" var="addInfo">
                                <h:outputLabel value="#{addInfo.additionalInfoType.acronym}: "/>
                                <h:outputText value="#{addInfo.value}"/>
                            </ui:repeat>
                        </h:panelGrid>
                    </p:rowExpansion>
                </p:dataTable>
                <p:commandButton value="OK"
                                 oncomplete="searchHuman.hide();"
                                 ajax="true" process="@form"
                                 action="#{newActorMB.confirmSearch}"
                                 update=":newHumanF:newHuman:actorTab:filiation, :newHumanF:newMsg"/>
                <p:commandButton value="Cancelar" onclick="searchHuman.hide();" immediate="true"/>
            </h:form>
        </p:dialog>

        <p:dialog id="addHumanDialog" position="top" modal="true" dynamic="true"
                  widgetVar="addHuman" closable="true" resizable="true"
                  width="1000" height="500">
            <h:form id="addHumanForm">
                <p:growl/>
                <util:humanRegister hParent="true" human="#{newActorMB.parent}"
                                    button="#{newActorMB.createParent(newActorMB.parent)}"/>
                <p:commandButton value="Cadastrar" process="@form" update="@form :newHumanF:newHuman:actorTab:filiation" onsuccess="addHuman.hide()"
                                 action="#{newActorMB.createParent(newActorMB.parent)}"/>
            </h:form>
        </p:dialog>
        
        <script type="text/javascript">  
            function handleSubmitRequest(xhr, status, args) {
                if(!args.validationFailed) {
                    addHuman.hide();
                }
            }
        </script>
    </ui:define>
</ui:composition>